package com.example.myfirstapp;

import android.content.Context;

import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.content.ContentValues;

public class CategoriesHelper extends SQLiteOpenHelper {
	public static final String TABLE_CATEGORIES = "categories";
	public static final String COLUMN_ID = "_id";
	public static final String COLUMN_MAIN = "main";
	public static final String COLUMN_SUB = "sub";
	public static final String COLUMN_OPERATION = "operation";

	public static final String DATABASE_NAME = "operations.db";
	public static final int DATABASE_VERSION = 2;

	private static final String DATABASE_CREATE = "CREATE TABLE " +
		TABLE_CATEGORIES + "(" +
		COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
		COLUMN_MAIN + " TEXT NOT NULL, " +
		COLUMN_SUB + " TEXT NOT NULL, " +
		COLUMN_OPERATION + " INTEGER NOT NULL);";

	public CategoriesHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase database) {
		database.execSQL(DATABASE_CREATE);
		String [] mainCategories = {"Alimentação", "Moradia"};
		String [][] subCategories = {{"Supermercado", "Almoço", "Lanche"},
			{"Aluguel", "Luz"}};

		ContentValues values = new ContentValues();
		for (int i = 0; i < mainCategories.length; i++) {
			for (int j = 0; j < subCategories[i].length; j++) {
				values.put(COLUMN_MAIN, mainCategories[i]);
				values.put(COLUMN_SUB, subCategories[i][j]);
				values.put(COLUMN_OPERATION, Operations.DEBIT);
				database.insert(TABLE_CATEGORIES, null, values);
			}
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase database, int oldVersion,
			int newVersion) {
		database.execSQL("DROP TABLE IF EXISTS " + TABLE_CATEGORIES);
		onCreate(database);
	}
}
